﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace com.huigo.B2B.Web
{
    public partial class VerifyImagePage : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            string fileformat = DateTime.Now.Ticks.ToString();
            Random validFeed = new Random();
            int nLength = 0;
            for (int i = 0; i < 10; i++)
            {
                nLength = validFeed.Next(fileformat.Length);
            }

            if (nLength < fileformat.Length - 5)
                fileformat = fileformat.Substring(nLength, 5);
            else
                fileformat = fileformat.Substring(fileformat.Length - 5, 5);

            int current = Convert.ToInt32(fileformat);
            current = current * validFeed.Next(1, 200);

            if (current == 0 || current.ToString().Length < 5) current = 10101;
            fileformat = current.ToString().Substring(0, 4);


            HttpCookie cookie = new HttpCookie("vvvvalidccccode", fileformat);
            cookie.HttpOnly = true;
            cookie.Expires = DateTime.Now.AddHours(1d);
            HttpContext.Current.Response.Cookies.Add(cookie);

            Bitmap bitmap = new Bitmap(60, 22);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);

            // 背景噪点
            Random random = new Random();
            for (int i = 0; i < 25; i++)
            {
                int x1 = random.Next(bitmap.Width);
                int x2 = random.Next(bitmap.Width);
                int y1 = random.Next(bitmap.Height);
                int y2 = random.Next(bitmap.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            // 绘制验证码
            Font font = new System.Drawing.Font("Verdana", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bitmap.Width, bitmap.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(fileformat, font, brush, 2, 2);

            // 前景噪点
            for (int i = 0; i < 50; i++)
            {
                int x = random.Next(bitmap.Width);
                int y = random.Next(bitmap.Height);

                bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            // 边框
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);

            HttpContext.Current.Response.ContentType = "image/Gif";
            bitmap.Save(base.Response.OutputStream, ImageFormat.Gif);

            g.Dispose();
            g = null;

            bitmap.Dispose();
            bitmap = null;
        }
    }
}
